/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useRef } from 'react';
import DragTable from '@/components/DragTable';
import ProTable from '@ant-design/pro-table';
import KbRefuseModal from '@/components/RefuseModal';
import { message, Popconfirm, Space } from 'antd';
import type { ProColumns, ActionType } from '@ant-design/pro-table';
import type { PackageList, PackageProps } from '../utils/index.d';
import type { ConnectState } from '@/models/connect';
import { deductMoneyUntied } from '@/services/sms/blackDeductMoney';
import { getStorageLocList, approveStorageLoc } from '@/services/kdy/package';
import { useSelector } from 'umi';
import styles from './index.less';

const Index: React.FC<PackageProps> = (props) => {
  const { type } = props;
  const actionRef = useRef<ActionType>();
  const { winSize } = useSelector((state: ConnectState) => state.global);
  const handleRequest = async (params: any) => {
    console.log(params, '---');
    const { current: page_num, pageSize: page_size, ...rest } = params;
    const { code, data, msg } = await getStorageLocList({
      ...rest,
      page_num,
      page_size,
      status: type,
    });
    if (code == 0) {
      return { ...data, data: data.data };
    } else {
      message.error(msg);
    }
  };
  const handleBlackClick = async (record: PackageList, status: string) => {
    const { id } = record;
    const data: {
      id: string;
      status: string;
    } = {
      id,
      status,
    };
    const { code, msg } = await approveStorageLoc(data);
    if (code == 0) {
      actionRef.current?.reload();
      message.success('操作成功');
      return;
    }
    message.error(msg);
  };
  const handleUntied = async (record: PackageList) => {
    const { cm_id } = record;
    deductMoneyUntied({ kb_id: 0, cm_id }).then((res) => {
      const { code, msg } = res;
      if (code == 0) {
        actionRef.current?.reload();
        message.success('操作成功');
        return;
      }
      message.error(msg);
    });
  };
  const columns: ProColumns<PackageList>[] = [
    {
      title: '',
      dataIndex: 'query',
      hideInTable: true,
      valueType: 'text',
      fieldProps: {
        placeholder: '快递员手机号/位置名称',
      },
    },
    {
      title: 'id',
      dataIndex: 'id',
      align: 'center',
      search: false,
      ellipsis: true,
    },
    {
      title: '位置名称',
      dataIndex: 'title',
      align: 'center',
      search: false,
      ellipsis: true,
    },
    {
      title: '详细地址',
      dataIndex: 'location',
      align: 'center',
      search: false,
      ellipsis: true,
    },
    {
      title: '快递员账号',
      dataIndex: 'cm_phone',
      align: 'center',
      search: false,
      ellipsis: true,
    },
    {
      title: '修改时间',
      dataIndex: 'create_time',
      align: 'center',
      search: false,
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'options',
      align: 'center',
      search: false,
      hideInTable: type === 'reject',
      ellipsis: true,
      width: winSize === 'xs' ? 80 : 230,
      fixed: 'right',
      render: (_: any, record: PackageList) => {
        const { cm_id, cm_phone, is_deduct_not_send, id } = record;
        return (
          <Space direction={winSize === 'xs' ? 'vertical' : 'horizontal'}>
            <Popconfirm
              title={`你确定通过吗？`}
              onConfirm={handleBlackClick.bind(null, record, 'approved')}
              key='approved'
            >
              <a>通过</a>
            </Popconfirm>
            <Popconfirm
              title={`你确定驳回吗？`}
              onConfirm={handleBlackClick.bind(null, record, 'reject')}
              key='approved'
            >
              <a>驳回</a>
            </Popconfirm>
            {is_deduct_not_send ? (
              <Popconfirm
                title='你确定恢复下发吗？'
                onConfirm={handleUntied.bind(null, record)}
                key='reject'
              >
                <a>恢复下发</a>
              </Popconfirm>
            ) : (
              <KbRefuseModal
                label='确定扣钱不发？'
                api='/Shield/adminBlack'
                callback={handleBlackClick.bind(null, record, 'approved')}
                formData={{
                  kb_id: 0,
                  cm_id,
                  cm_phone,
                  locId: id,
                }}
              >
                <a>扣钱不发</a>
              </KbRefuseModal>
            )}
          </Space>
        );
      },
    },
  ];
  return (
    <DragTable>
      <ProTable<PackageList>
        actionRef={actionRef}
        rowKey='id'
        request={handleRequest}
        columns={columns}
        className={styles.smsSearch}
        pagination={{
          pageSize: 20,
          showQuickJumper: false,
          showSizeChanger: false,
        }}
        scroll={{ x: 640 }}
        options={false}
        search={{
          collapsed: false,
        }}
      />
    </DragTable>
  );
};

export default Index;
